//
//  LRXPocketLoadData.m
//  路人侠
//
//  Created by zhangxulong on 5/28/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "simpleNetTask.h"
@interface simpleNetTask()
@property (nonatomic,copy) NSString * urlStr;
@end

@implementation simpleNetTask

- (instancetype)initWithLoadDataBlock:(simpleNetTaskCallBackBlock)loadDataBlock
{
    self = [super init];
    if (self) {
        self.loadDataBlock =[loadDataBlock copy];
    }
    return self;
}
- (void)sessionDataTask:(NSDictionary *)parms withUrl:(NSString *)urlStr withMethod:(NSString *)method
{
    [MBProgressHUD showMessage:nil];
    
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    if ([method isEqualToString:@"GET"]) {
        [manger GET:urlStr parameters:parms progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUD];
                self.loadDataBlock(responseObject);
                
            });
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"网络连接失败"];
            NSLog(@"%@",error);
        }];
    }
    else if([method isEqualToString:@"POST"])
    {
        [manger POST:urlStr parameters:parms progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUD];
                self.loadDataBlock(responseObject);
                
            });
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"网络连接失败"];
        }];
    }
}@end
